//
//  ResetPassordViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/5.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ResetPassordViewController.h"
#import "LoginViewController.h"
#import "AccountManager.h"
@interface ResetPassordViewController ()
@property (assign,nonatomic) int timeout;
@property (strong,nonatomic) NSTimer* countDownTimer;
@end

@implementation ResetPassordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgView1.layer.cornerRadius=5;
    self.bgView1.layer.borderWidth=0.5;
    self.bgView1.layer.borderColor=[UIColor colorWithHexString:@"d8d8d8"].CGColor;
    self.bgView2.layer.cornerRadius=5;
    self.bgView2.layer.borderWidth=0.5;
    self.bgView2.layer.borderColor=[UIColor colorWithHexString:@"d8d8d8"].CGColor;
    self.bgView3.layer.cornerRadius=5;
    self.bgView3.layer.borderWidth=0.5;
    self.bgView3.layer.borderColor=[UIColor colorWithHexString:@"d8d8d8"].CGColor;
    self.getCodeBt.layer.cornerRadius=5;
    self.resetBt.layer.cornerRadius=5;
    self.codeTF.delegate=self;
    [self setNaviTitle:@"重置密码"];
    [self setNaviLeftButton:@""];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)clickLeftButton:(UIButton*)sender{
    if (self.navigationController.viewControllers.count>1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark-获取验证码
- (IBAction)getCodeAction:(id)sender {
    
    if (![self.mobileTF.text isMobileNumber]) {
        [[LKDHud shareHud] showtip:@"请输入正确的手机号"];
        return;
    }
     [[LKDHud shareHud] showLoadingView];
    [ApiClient sendForgottenSmsTelephone:self.mobileTF.text didFinishLoaded:^(KXJson *json, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        if ([[json getStringForKey:@"status"] intValue]==1) {
            [self timeFire];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
    
}
- (void)logout {
    [[LKDHud shareHud] showLoadingView];
    [ApiClient loginOutDidFinishLoaded:^(KXJson *json, NSString *path) {
        NSString *status = [json getStringForKey:@"status"];
        [[LKDHud shareHud] hideLoadingView];
        if ([status isEqualToString:@"1"]) {
            [APP_CONTEXT.accountManager logout];
            [self.navigationController popToRootViewControllerAnimated:YES];
            //  [[LKDHud shareHud] showtip:@"成功推出登陆"];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        [[LKDHud shareHud] hideLoadingView];
    }];
}

#pragma mark-重置密码
- (IBAction)resetPasswordAction:(id)sender {
    [self.mobileTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    if (![self.mobileTF.text isMobileNumber]) {
        [[LKDHud shareHud] showtip:@"请输入正确的手机号"];
        return;
    }
    if (self.codeTF.text.length==0) {
        [[LKDHud shareHud] showtip:@"验证码不能为空"];
        return;
    }
    if (self.passwordTF.text.length==0) {
        [[LKDHud shareHud] showtip:@"密码不能为空"];
        return;
    }
     [[LKDHud shareHud] showLoadingView];
    [ApiClient forgetPasswordWithPhone:self.mobileTF.text sms:self.codeTF.text password:self.passwordTF.text didFinishLoaded:^(KXJson *json, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        if ([[json getStringForKey:@"status"] intValue]==1) {
            if (self.navigationController.viewControllers.count>1) {
                [self logout];
            }else{
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }
        }
        
        
        
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
}


- (void)timeFire
{
    if (!self.getCodeBt.isEnabled) {
        return;
    }
    
    _timeout=59; //倒计时时间
    [self.getCodeBt  setEnabled:NO];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}

-(void)timeFireMethod{
    if (_timeout > 0) {
        _timeout--;
    }else{
        _timeout = 0;
    }
    
    if(_timeout==0){
        [self.getCodeBt setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.getCodeBt setEnabled:YES];
        [_countDownTimer invalidate];
    }else{
        int seconds = _timeout % 60;
        [self.getCodeBt setEnabled:NO];
        NSString *strTime = [NSString stringWithFormat:@"%.2ds", seconds];
        self.getCodeBt.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.getCodeBt.titleLabel.text = strTime;
        [self.getCodeBt setTitle:strTime  forState:UIControlStateNormal];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location >= 6)
        return NO; // return NO to not change text
    return YES;
}
@end
